﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickURLShortner.Common.Models;
using QuickURLShortner.Common;
using QuickURLShortner.Common.Utils;

namespace QuickURLShortner.Controllers
{
    public class ShortURLController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        // POST: /ShortURL/Create
        public ActionResult UrlShortner(UrlModel model)
        {
            try
            {
                #region "Create"
                var oShortUrl = new Container();

                oShortUrl.RealUrl = model.OrignalUrl;
                oShortUrl.ShortenedUrl = Utils.UniqueShortUrl();
                oShortUrl.CreateDate = DateTime.Now;
                oShortUrl.CreatedBy = "d4g";

                Utils.AddUrlToDatabase(oShortUrl);

                oShortUrl.ShortenedUrlCombo = Utils.PublicShortUrl(oShortUrl.ShortenedUrl);

                #endregion
                // Add insert logic here
                ViewData[UrlConstants.URL_PART1] = oShortUrl.ShortenedUrlCombo.FirstPart;
                ViewData[UrlConstants.URL_PART2] = oShortUrl.ShortenedUrlCombo.SecondPart;

                return View(UrlConstants.VIEW_INDEX2);
            }
            catch (Exception ex)
            {
                // todo proper error logging 
                return View(UrlConstants.VIEW_INDEX2);
            }
        }
    }
}
